package personal.wang.juc;


import java.io.IOException;
import java.util.concurrent.*;

/**
 * @className: DelayRecurse
 * @Description:
 * @Author: wangyifei
 * @Date: 2025/3/21 9:38
 */
public class DelayRecurse {
    public static void main(String[] args) throws IOException {
        DO aDo = new DO(){

            @Override
            public Integer execute() {
                return 1;
            }
        };
        System.out.println(aDo.doTry(10));
//        System.in.read();
    }
    public static abstract class DO<T , TT> {

        public abstract T execute();

        public T doTry(int cnt) {
            return doTry(cnt , cnt);
        }
        public T doTry(int cnt , int sum) {
            ScheduledFuture<T> schedule = null ;
            if(cnt > 0){
                ScheduledExecutorService pool = Executors.newScheduledThreadPool(3);
                System.out.println("execute " + (sum - cnt + 1));
                if(cnt == 1){
                    return  execute();
                }
                schedule = pool.schedule(() -> this.doTry(cnt - 1 , sum), 2, TimeUnit.SECONDS);
            }
            try {
                return schedule.get();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
